package com.sinodata.bsm.cicp.collector.was7;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.JmxUtil;
import com.sinodata.bsm.common.vo.ResPropertyValue;

public class WebAppWas7Collector extends Collector {

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> value = new ArrayList<ResPropertyValue>();
        ResPropertyValue propertyValueX = new ResPropertyValue();
        String[] propMetas = getPropMeta();
        Object stats;
        ResPropertyValue.PropValue[] proValues = new ResPropertyValue.PropValue[getPropMeta().length];
        String serverName = this.getParamMap().get("serverName");
        String webAppName = this.getParamMap().get("webAppName");
        String nodeName = this.getParamMap().get("nodeName");
        JMXConnector connector = null;
        try {
            String query = "WebSphere:type=WLMAppServer,name=" + webAppName + ",process=" + serverName + ",node=" + nodeName + ",*";
            ObjectName queryName = new ObjectName(query);
            //            connector = JmxUtil.getConnection(JmxUtil.WEBSPHERE7, ip + ":" + this.getParamMap().get("serverPort"));
            connector = JmxUtil.getConnection(ip, Integer.parseInt(this.getParamMap().get("serverPort")));
            MBeanServerConnection mbsc = connector.getMBeanServerConnection();
            Set s = mbsc.queryNames(queryName, null);
            for (Iterator obj = s.iterator(); obj.hasNext();) {
                ObjectName nodeAgent = (ObjectName) obj.next();
                stats = mbsc.getAttribute(nodeAgent, "stats");
            }

        } catch (Exception e) {
            logger.error("failed to collect by JVMCollector", e);
        } finally {
            JmxUtil.closeConnector(connector);
        }
        return null;
    }

    @Override
    public String[] getParamMeta() {
        //        return new String[] { "服务器名", "服务器端口", "节点名", "应用管理器名" };
        return new String[] { "serverName", "serverPort", "nodeName", "webAppName" };
    }

    @Override
    public String[] getPropMeta() {
        //        return new String[] { "加载servlet数", "请求数", "总服务时间" };
        return new String[] { "LoadServletNum", "RequestsNum", "TotalServiceTime" };
    }

}
